import pymysql

from conn_mysql import ConnectMysql

import re

import datetime

class ApiVerify(ConnectMysql):

    def openid_notify_code(self,openid,verify_code):

        sql = 'select * from phone_notify_code where openid=%s and phone_notify_code=%s'

        result_info = self.select_one(sql, (openid, verify_code))

        return result_info

    def verify_code(self, methods, openid, verify_code):
        '''验证码发送'''

        if not methods == 'GET':
    
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not all((openid, verify_code)):

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':f'请检查参数是否为空,openid:{openid},verify_code{verify_code}请重新输入',
                'resultBody':None
            }

        openid_notify_code = self.openid_notify_code(openid, verify_code)

        if not openid_notify_code:

            return {
                    'status': -1,
                    'errorCode': 2,
                    'errorMsg':'验证参数是否正确openid:{openid},verrity_code:{verify_code}',
                    'resultBody':None
                }

        else:

            regedate = openid_notify_code['regedate']

            now = datetime.datetime.now()

            time_line = datetime.timedelta(minutes=5,hours=8)

            if now - regedate <= time_line:

                return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':'验证通过'
                    }

            else:
                
                return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':'验证通过'
                    }





        

    